package com.cdxy.service.impl;

import com.cdxy.dmr.dataObject.AxUserDO;
import com.cdxy.dmr.dataObject.AxUserProfileDO;
import com.cdxy.dmr.repository.AxUserProfileRepository;
import com.cdxy.dmr.repository.AxUserRepository;
import com.cdxy.enums.BizErrorEnum;
import com.cdxy.enums.KaptchaEnum;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.req.ModifyBindPhoneReq;
import com.cdxy.model.dto.req.ModifyUserInfoReq;
import com.cdxy.service.IAxUserService;
import com.cdxy.service.ISmsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author autoGenCode
 * @since 2020-03-05
 */
@Service
public class AxUserServiceImpl implements IAxUserService {

    @Autowired
    private ISmsService smsService;

    @Autowired
    private AxUserProfileRepository axUserProfileRepository;

    @Autowired
    private AxUserRepository axUserRepository;

    @Override
    public void modifyUserInfo(ModifyUserInfoReq modifyUserInfoReq) {
        String avatar = modifyUserInfoReq.getAvatar();
        String nickname = modifyUserInfoReq.getNickname();
        Integer sex = modifyUserInfoReq.getSex();
        LocalDate birthDay = modifyUserInfoReq.getBirthDay();
        String hobbit = modifyUserInfoReq.getHobbit();
        Integer axUid = modifyUserInfoReq.getAxUid();
        String selfhoodSign = modifyUserInfoReq.getSelfhoodSign();
        String areas = modifyUserInfoReq.getAreas();

        if (StringUtils.isBlank(avatar) && StringUtils.isBlank(nickname) && sex == null && birthDay == null && StringUtils.isBlank(hobbit) && StringUtils.isBlank(selfhoodSign) && StringUtils.isBlank(areas)) {
            return;
        }

        AxUserProfileDO axUserProfileDO = axUserProfileRepository.getById(axUid);
        AxUserProfileDO axUserProfileUpdateDO = new AxUserProfileDO();
        int flag = 0;
        if (StringUtils.isNotBlank(avatar) && !avatar.equals(axUserProfileDO.getAvatar())) {
            flag++;
            axUserProfileUpdateDO.setAvatar(avatar);
        }
        if (StringUtils.isNotBlank(nickname) && !nickname.equals(axUserProfileDO.getNickname())) {
            flag++;
            axUserProfileUpdateDO.setNickname(nickname);
        }
        if (StringUtils.isNotBlank(hobbit) && !hobbit.equals(axUserProfileDO.getHobbit())) {
            flag++;
            axUserProfileUpdateDO.setHobbit(hobbit);
        }
        if (sex != null && !sex.equals(axUserProfileDO.getSex())) {
            flag++;
            axUserProfileUpdateDO.setSex(sex);
        }
        if (birthDay != null && !birthDay.equals(axUserProfileDO.getBirthDay())) {
            flag++;
            axUserProfileUpdateDO.setBirthDay(birthDay);
        }
        if (StringUtils.isNotBlank(selfhoodSign) && !selfhoodSign.equals(axUserProfileDO.getSelfhoodSign())) {
            flag++;
            axUserProfileUpdateDO.setSelfhoodSign(selfhoodSign);
        }
        if (StringUtils.isNotBlank(areas) && !areas.equals(axUserProfileDO.getAreas())) {
            flag++;
            axUserProfileUpdateDO.setAreas(areas);
        }

        if (flag > 0) {
            axUserProfileUpdateDO.setAxUid(axUid);
            axUserProfileRepository.updateById(axUserProfileUpdateDO);
        }
    }

    @Override
    public void modifyBindPhone(ModifyBindPhoneReq modifyBindPhoneReq) {
        String phone = modifyBindPhoneReq.getPhone();
        String code = modifyBindPhoneReq.getCode();
        Integer axUid = modifyBindPhoneReq.getAxUid();

        AxUserDO axUserDO = axUserRepository.getByMobile(phone);
        if (axUserDO != null) {
            throw new BusinessException(BizErrorEnum.PHONE_HAD_BIND);
        }

        smsService.verifySms(KaptchaEnum.BusinessCode.MODIFY_BIND_PHONE, phone, code);
        axUserDO = new AxUserDO();
        axUserDO.setMobile(phone);
        axUserDO.setAxUid(axUid);
        axUserRepository.updateById(axUserDO);

    }
}
